#include "ros/ros.h"
#include "msgs_client_server/AddInt.h"

int main(int argc, char *argv[])
{
    setlocale(LC_ALL, "");

    if(argc != 3)
    {
        ROS_ERROR("Usage: demo_client num1 num2");
        return 1;
    }
    ros::init(argc, argv, "client");
    ros::NodeHandle nh;
    // 客户端只用说自己想要连接的服务器提交的服务名称
    ros::ServiceClient client = nh.serviceClient<msgs_client_server::AddInt>("AddInt");

    // 优化：等待服务启动成功
    // 1.
    // client.waitForExistence();

    // 2.
    ros::service::waitForService("AddInt");

    // 设置参数，开始调用
    msgs_client_server::AddInt ai;
    ai.request.num1 = atoi(argv[1]);
    ai.request.num2 = atoi(argv[2]);

    bool flag = client.call(ai);
    if(flag)
    {
        ROS_INFO("结果已经收到：%d", ai.response.sum);
    }
    else
    {
        ROS_ERROR("调用失败，请检查参数");
    }

    return 0;
}
